﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using GenaralLibs;
using System.Data.SqlClient;

namespace Lists
{
    public class DMNganhNghe_TieuThuc: DoiTuong
    {
        
        #region NganhNgheID property
        public int NganhNgheID
        {
            get
            {
                return (int)_DataRow["NganhNgheID"];
            }
            set
            {
                _DataRow["NganhNgheID"] = value;
            }
        }
        #endregion
        #region TieThucID property
        public int TieuThucID
        {
            get
            {
                return (int)_DataRow["TieuThucID"];
            }
            set
            {
                _DataRow["TieuThucID"] = value;
            }
        }
        #endregion

        public DMNganhNghe_TieuThuc(DataRow dataRow)
            : base(dataRow)
        {
        }


    }

    public class DanhMucNganhNghe_TieuThuc: DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucNganhNghe_TieuThuc _Instance;
        public static DanhMucNganhNghe_TieuThuc Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucNganhNghe_TieuThuc();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "NganhNghe_TieuThuc";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new DMNganhNghe_TieuThuc((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public DMNganhNghe_TieuThuc this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new DMNganhNghe_TieuThuc(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public DMNganhNghe_TieuThuc NewDoiTuong()
        {
            try
            {
                return new DMNganhNghe_TieuThuc(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        public static DanhMucNganhNghe_TieuThuc Filter(string filterExpression)
        {
            try
            {
                DanhMucNganhNghe_TieuThuc obj = new DanhMucNganhNghe_TieuThuc();
                obj.DataView = new DataView(DanhMucNganhNghe_TieuThuc.Instance.DataTable, filterExpression, "ID", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        public DMNganhNghe_TieuThuc GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new DMNganhNghe_TieuThuc(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        public static DataTable GetTieuThucByNganhNgheID(int nganhNgheID)
        {
            SqlConnection con = new SqlConnection(GenaralLibs.LocalSettings.ConnectString);
            try
            {
                con.Open();

                SqlCommand cmd = con.CreateCommand();
                DataSet ds = new DataSet();
                ds.Clear();
                cmd.CommandText = string.Format("Select * from TieuThucQuanLy where ID in(select TieuThucID from NganhNghe_TieuThuc where NganhNgheID = '{0}')",
                                                    nganhNgheID);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);

                return ds.Tables[0];
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(GenaralLibs.CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
            finally
            {
                con.Close();
            }
        }
        public static DanhMucNganhNghe_TieuThuc GetByNganhNgheID(int nganhNgheID)
        {
            try
            {
                return Filter(string.Format("NganhNgheID= '{0}'", nganhNgheID));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        public static DMNganhNghe_TieuThuc GetByNganhNgheTieuThuc(int nganhNgheID,int tieuThucID)
        {
            try
            {
                return Filter(string.Format("NganhNgheID= '{0}' and TieuThucID= '{1}'", nganhNgheID,tieuThucID))[0];
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        public static void DeleteFromDB(int nganhNgheID)
        {
            SqlConnection con = new SqlConnection(GenaralLibs.LocalSettings.ConnectString);
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandText = string.Format("DELETE FROM NganhNghe_TieuThuc WHERE NganhNgheID = '{0}'",
                                                    nganhNgheID);

                cmd.ExecuteNonQuery();
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(GenaralLibs.CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
            finally
            {
                con.Close();
            }
        }

        public DMNganhNghe_TieuThuc GetByID(int objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucNganhNghe_TieuThuc GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}